$("#find").click(function() {
    find.doFind();
});
// 获取短信
$("#getCode").click(function() {
    find.sendCode();
});
var find = {
    doFind: function() {
        this.password = $("#password").val();
        this.password1 = $("#password1").val();
        this.phone = $("#phone").val();
        this.smscode = $("#code").val();
        this.check();
        if (this.flog) {
            $.post(
                "/findpwd/findpwd", {
                    password: this.password,
                    smscode: this.smscode,
                    phone: this.phone
                },
                function(reg) {
                    if (reg.status == "success") {
                        layer.msg(reg.data, {
                            icon: 1
                        })
                        setTimeout(function() {
                            window.location.href = "/login";
                        }, 1500);

                    } else if (reg.status == "failed") {
                        layer.msg(reg.data, {
                            icon: 2
                        })
                    }
                }
            );
        }
    },
    sendCode: function() {
        var that = this;
        this.phone = $("#phone").val();
        if (this.phone.length == 11) {
            $.post(
                "/findpwd/getSmsCode", {
                    phone: this.phone
                },
                function(reg) {
                    if (reg.status == "success") {
                        layer.msg(reg.data, {
                            icon: 1
                        })
                        that.disableButton();
                    } else if (reg.status == "failed") {
                        //that.isStartCheckCode = true;//------------
                        layer.msg(reg.data, {
                            icon: 2
                        })
                    }
                }
            );
        } else {
            layer.tips("手机号码位数不对", "#phone");
            $("#phone").focus();
        }

    },
    check: function() {
        //提交给服务器前的验证
        if (this.password.length < 6) {
            layer.tips("密码不能少于6位", "#password");
            $("#password").focus();
        } else if (this.phone.length != 11) {
            layer.tips("手机号码尾数不对", "#phone");
            $("#phone").focus();
        } else if (this.smscode.length == 0) {
            layer.tips("验证码不能为空", "#code");
            $("#code").focus();
        } else if (this.password != this.password1) {
            layer.tips("两次输入密码不一致", "#password");
            $("#code").focus();
        } else {
            this.flog = true;
        }
    },
    disableButton: function() {
        $("#getCode").attr('disabled', "disabled");
        var timestamp = new Date().getTime(); //得到当前时间戳  毫秒级别
        localStorage.codeTime1 = timestamp;
        this.recoveryButton();
    },
    recoveryButton: function(ts) {
        console.log(1);
        var that = this;
        var timestamp1 = new Date().getTime();
        ts = timestamp1 - parseInt(localStorage.codeTime1);
        $("#getCode").attr('disabled', "disabled");
        if (ts < 60000) {
            $("#getCode").text(60 - parseInt(ts / 1000) + "S");
            setTimeout(function() {
                --ts;
                that.recoveryButton(ts);
            }, 1000);

        } else {
            $("#getCode").removeAttr('disabled');
            $("#getCode").blur();
            $("#getCode").text("获取短信验证码");
        }
    }
};

find.recoveryButton();
